package com.hs.kfk.boot;

import com.alibaba.fastjson2.JSON;
import com.hs.kfk.serializer.User;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @Description: kafka整合SpringBoot 消息消费者
 * @Author 胡尚
 * @Date: 2024/8/8 17:09
 */
@Component
public class BootConsumer {

    @KafkaListener(topics = {"disTopic"})
    public void consumerMessage(ConsumerRecord<String, String> record){
        int partition = record.partition();
        long offset = record.offset();
        String topic = record.topic();
        String key = record.key();
        String message = record.value();
        System.out.println("topic:" + topic + "\tpartition:" + partition + "\toffset: " + offset + "\tkey: " + key + "\tmessage: " + message);
        User user = JSON.parseObject(message, User.class);
        System.out.println(user);
    }
}
